package com.jiangli.controller.user;


import com.jiangli.annotation.RateLimit;
import com.jiangli.result.PageResult;
import com.jiangli.result.Result;
import com.jiangli.service.DishService;
import com.jiangli.vo.DishBriefVO;
import com.jiangli.vo.DishRatingVO;
import com.jiangli.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;


@RestController("userDishController")
@RequestMapping("/user/dish")
@Slf4j
@Api(tags = "C端-菜品浏览接口")
public class DishController {
    private final DishService dishService;

    @Autowired
    public DishController(DishService dishService) {
        this.dishService = dishService;
    }
    @RateLimit
    @GetMapping("/list")
    @ApiOperation("首页获取菜品接口")
    public Result<PageResult> getDishList(@RequestParam(defaultValue = "1") Integer page,
                                                @RequestParam(defaultValue = "10") Integer pageSize) {
        log.info("获取菜品第几页{},页数大小{}",page,pageSize);
        PageResult pageResult = dishService.getDishList(page,pageSize);
        return Result.success(pageResult);
    }
    @RateLimit
    @GetMapping("/recommend")
    @ApiOperation("获取个性化推荐菜品")
    public Result<PageResult> getRecommendedDishes(
            @RequestParam Long userId,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(defaultValue = "false") boolean reset) {
        log.info("获取个性化推荐userid{}",userId);
        return Result.success(dishService.getRecommendedDishByPage(userId, page, pageSize, reset));
    }
    @RateLimit
    @GetMapping("/random")
    @ApiOperation("获取随机菜品，参数是获取菜品类型")
    public Result<DishBriefVO> getRandomDishes(@RequestParam Integer mealType) {
        log.info("获取随机菜品");
        Integer count = 1;
        DishBriefVO dishVO = dishService.getDishRandom(mealType,count);
        return Result.success(dishVO);
    }
    @RateLimit
    @GetMapping("/getDetail")
    @ApiOperation("获取单个菜品详情包括菜品食材")
    public Result<DishVO> getDishDetail(@RequestParam Integer id) {
        DishVO dishVO = dishService.getDishDetailById(Long.valueOf(id));
        return Result.success(dishVO);
    }
    @RateLimit
    @PostMapping("/rate")
    @ApiOperation("评分")
    public ResponseEntity<String> submitRating(
            @RequestParam Long userId,
            @RequestParam Long dishId,
            @RequestParam Double score) {
        try {
            boolean success = dishService.rateDish(userId, dishId, score);
            if (success) {
                return ResponseEntity.ok("评分成功");
            } else {
                return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("评分失败");
            }
        } catch (IllegalArgumentException e) {
            return ResponseEntity.badRequest().body(e.getMessage());
        }
    }

    @RateLimit
    @GetMapping("/rate")
    @ApiOperation("获取用户评价信息"
    )
    public Result<DishRatingVO> getRating(@RequestParam Long userId, @RequestParam Long dishId) {
        DishRatingVO dishRatingVO = dishService.getUserRating(userId,dishId);
        return Result.success(dishRatingVO);
    }
    @RateLimit
    @GetMapping("/rank")
    @ApiOperation("菜品排行")
    public Result<PageResult> getRantDishes(@RequestParam(defaultValue = "1") Integer page,
                                             @RequestParam(defaultValue = "10") Integer pageSize,
                                             @RequestParam(defaultValue = "false") boolean reset) {
        PageResult pageResult = dishService.getRank(page,pageSize,reset);
        return Result.success(pageResult);
    }
    @RateLimit
    @GetMapping("/calorieRank")
    @ApiOperation("菜品卡路里排行")
    public Result<PageResult> getCalorieRank(@RequestParam(defaultValue = "1") Integer page,
                                             @RequestParam(defaultValue = "10") Integer pageSize,
                                             @RequestParam(defaultValue = "false") boolean reset){
        PageResult pageResult = dishService.getCalorieRank(page,pageSize,reset);
        return Result.success(pageResult);
    }
}
